package com.github_Dididipapa.shortlink.project.controller;

import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import com.github_Dididipapa.shortlink.project.service.LinkService;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @description: To be defined
 * @author: dididi_papa
 * @email: mitis1624087624@gmail.com
 * @date: 2025/09/17  14:15
 */
@RestController
@RequiredArgsConstructor
public class RouterLinkController {

    private final LinkService linkService;

    /**
     * @description: 短链接跳转原始链接
     * @author: dididi_papa
     * @date: 2025/9/17 下午9:25
     * @param: [shortLinkUri, request, response]
     * @return: void
     **/
    @GetMapping("/restore/{short-link}")
    public void routerLink(@PathVariable("short-link") String shortLinkUri,
                           ServletRequest request,
                           ServletResponse response) throws IOException {
        linkService.routerLink(shortLinkUri, request, response);
    }

    /**
     * 提取后缀路径
     */
    private String extractSuffix(String requestURI, String shortLink) {
        String prefix = "/restore/" + shortLink + "/";
        if (requestURI.startsWith(prefix)) {
            return requestURI.substring(prefix.length());
        }
        return "";
    }
}
